package com.qsln.app.utils;

import java.io.File;
import java.util.List;
import java.util.Map;

import org.xutils.x;
import org.xutils.common.Callback.CommonCallback;
import org.xutils.common.task.PriorityExecutor;
import org.xutils.http.RequestParams;

import com.qsln.adapter.api.OnRequestFinishListener;

public class XUtilNet {
	public static void xutils(String surl,
			final OnRequestFinishListener listener, final String id) {

		RequestParams params = new RequestParams(surl);
		params.setExecutor(new PriorityExecutor(20));

		x.http().get(params, new CommonCallback<String>() {
			@Override
			public void onCancelled(CancelledException arg0) {
			}

			@Override
			public void onError(Throwable arg0, boolean arg1) {
				listener.onError();
			}

			@Override
			public void onFinished() {
			}

			@Override
			public void onSuccess(String arg0) {
				listener.onSuccess(arg0, id);
			}
		});
	}

	public static void xutils(String surl,
			final OnRequestFinishListener listener, final String id,
			Map<String, String> param) {
		RequestParams params = new RequestParams(surl);
		for (String key : param.keySet()) {
			params.addBodyParameter(key, param.get(key));

		}
		params.setExecutor(new PriorityExecutor(10));
		x.http().get(params, new CommonCallback<String>() {
			@Override
			public void onCancelled(CancelledException arg0) {
			}

			@Override
			public void onError(Throwable arg0, boolean arg1) {
				listener.onError();
			}

			@Override
			public void onFinished() {
			}

			@Override
			public void onSuccess(String arg0) {
				listener.onSuccess(arg0, id);
			}
		});
	}

	public static void xutilsFile(String surl,
			final OnRequestFinishListener listener, Map<String, Object> param) {
		RequestParams params = new RequestParams(surl);
		for (String key : param.keySet()) {
			if (key.equals("image")) {

				params.addParameter(key, param.get(key));
			}
			params.addParameter(key, param.get(key));

		}
		params.setExecutor(new PriorityExecutor(10));

		x.http().post(params, new CommonCallback<String>() {
			@Override
			public void onCancelled(CancelledException arg0) {
			}
			@Override
			public void onError(Throwable arg0, boolean arg1) {
			}

			public void onFinished() {
				
			}

			@Override
			public void onSuccess(String arg0) {
				listener.onSuccess(arg0, "");
			}
		});

	}

	public static void xutils1(String surl,
			final OnRequestFinishListener listener, Map<String, Object> param) {
		RequestParams params = new RequestParams(surl);
		for (String key : param.keySet()) {
			params.addParameter(key, param.get(key));
		}
		params.setExecutor(new PriorityExecutor(10));

		x.http().post(params, new CommonCallback<String>() {
			@Override
			public void onCancelled(CancelledException arg0) {
			}

			@Override
			public void onError(Throwable arg0, boolean arg1) {

			}

			@Override
			public void onFinished() {

				String s = "";
			}

			@Override
			public void onSuccess(String arg0) {
				listener.onSuccess(arg0, "");
			}
		});

	}
	/**
	 * 网络请求监听
	 * 
	 * @author ench-wu
	 * 
	 */
	public interface OnRequestFinishListener {
		void onError();
		void onSuccess(String s, String id);
	}
}
